﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;


namespace KL.Store.Core.Domain
{
    public class Mapper
    {
        #region MapDomainToViewModel

        public static void MapDomainToViewModel(object source, object destination)
        {
            #region Copy Field Values

            foreach (var fi in source.GetType().GetFields())
            {
                fi.SetValue(destination, fi.GetValue(source));
            }

            #endregion

            #region Copy Property Values

            foreach (var pr in source.GetType().GetProperties())
            {
                PropertyInfo prDestination = destination.GetType().GetProperty(pr.Name);

                //if Setter exists
                if (prDestination.GetSetMethod() != null)
                {
                    pr.SetValue(destination, pr.GetValue(source, null), null);
                }
            }

            #endregion
        }

        #endregion

        #region MapDomainToViewModel

        public static void MapDomainToViewModel2(object source, object destination)
        {
            #region Copy Field Values

            //foreach (var fi in source.GetType().GetFields())
            //{
            //    fi.SetValue(destination, fi.GetValue(source));
            //}

            #endregion

            #region Copy Property Values

            foreach (var prDestination in destination.GetType().GetProperties())
            {
                PropertyInfo pr = source.GetType().GetProperty(prDestination.Name);

                //if Setter exists
                if (prDestination.GetSetMethod() != null && pr != null)
                {
                    var sVal = pr.GetValue(source, null);
                    prDestination.SetValue(destination, sVal, null);
                }
            }

            #endregion
        }

        #endregion
    }
}
